package com.syni.mdd.yhd.common.all.controller.sms;

import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @className Test
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/4 0:35
 * @VERSION 1.0
 **/
@Log4j2
public class Test {

    Object o = new Object();

    Object sevice = new Object();
    private int a = 2000;

    @Transactional
    public void jian() {

        synchronized (this) {
            //读
            sevice.hashCode();
            //减
            a--;
            //保存
            sevice.toString();
            //
        }

        sevice.hashCode();
    }

    public void add() {
        synchronized (this) {
            a++;
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
////        CountDownLatch latch = new CountDownLatch(5);
//        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
//        for (int i = 0; i < 10; i++) {
//            new Thread(() -> {
////                try {
////                    cyclicBarrier.await();
////                } catch (InterruptedException e) {
////                    log.error(e.getMessage(),e);
////                } catch (BrokenBarrierException e) {
////                    log.error(e.getMessage(),e);
////                }
//                for (int j = 0; j < 200; j++) {
//                    test.jian();
//                }
//
////                latch.countDown();
//            }).start();
//        }
//
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        } catch (InterruptedException e) {
//            log.error(e.getMessage(),e);
//        }
//        //        try {
////            latch.await();
////        } catch (InterruptedException e) {
////            log.error(e.getMessage(),e);
////        }
//        System.out.println(test.a);

        new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                log.error(e.getMessage(),e);
            }
        }).start();
    }
}
